using DA.AnimGraph;
using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine.UIElements;

namespace DA.AnimGraph.Editor {
    internal abstract class GraphCanvasBase : ContextPaneBase {

        protected AnimGraphView graphView;
        internal GraphCanvasBase(ContextPaneBase parent) : base(parent) {
            graphView = new AnimGraphView(this, ageEnv);
            // 复制执行:OnCopy
            // 剪切执行:OnCopy + OnDeleteSelection
            // 粘贴执行:CanPaste + OnPaste
            graphView.deleteSelection += OnDeleteSelection;
            graphView.canPasteSerializedData = CanPaste;
            graphView.unserializeAndPaste = OnPaste;
            graphView.serializeGraphElements = OnCopy;

            graphView.StretchToParentSize();
            Add(graphView);
        }

        protected override void OnDrawUI() {
            ageEnv.serviceManager.AddService<GraphViewService>(graphView);
        }
        protected override void OnClearUI() {
            ageEnv.serviceManager.RemoveService<GraphViewService>(graphView);
        }

        /// <summary>
        /// 删除选中元素
        /// </summary>
        protected virtual void OnDeleteSelection(string operationName, GraphView.AskUser askUser) { }

        protected virtual string OnCopy(IEnumerable<GraphElement> elements) {
            return null; // 必须返回null
        }

        protected virtual bool CanPaste(string data) {
            return AGEGlobal.GetInstance().clipboard.HasCopy(AGEClipboard.CopyContentFormat.BlendTree);
        }

        protected virtual void OnPaste(string operationName, string data) {

        }

    }
}